package com.hsgene.shoppingcar.controller;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.MessageResponse;
import com.hsgene.shoppingcar.domain.ShoppingCarInfo;
import com.hsgene.shoppingcar.dto.ShoppingCarInfoDto;
import com.hsgene.shoppingcar.service.ShoppingCarService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: wangxuefeng@hsgene.com
 * @Description:
 * @Date: Created in 15:48 2018/7/16
 * @Modified By:
 */
@RestController
@RequestMapping("/shoppingcar")
public class ShoppingCarController {
    /**
     * LOG
     */
    private static Logger LOGGER = LogManager.getLogger(ShoppingCarController.class);

    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 加入购物车
     *
     * @param carInfo
     * @return
     */
    @PostMapping
    public ApiResult<Integer> addShoppingCar(@RequestBody ShoppingCarInfo carInfo) {
        try {
            //时间
            carInfo.setCreateTime(new Date());
            int result = shoppingCarService.insert(carInfo);
            LOGGER.info("add shopping car is suceess...");
            return new ApiResult(200, "success", "购物车添加成功", result);
        } catch (Exception e) {
            LOGGER.error("add shopping car is error", e);
            return new ApiResult(MessageResponse.ADD_SHOPPING_CAR_ERROR);
        }
    }

    /**
     * 购物车列表
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping
    public ApiResult<Map<String, Object>> queryShoppingCar(String userId) {
        try {
            Map<String, Object> result = shoppingCarService.queryShoppingCarByUserId(userId);
            LOGGER.info("query shopping car is suceess...");
            return new ApiResult(200, "success", "查询购物车列表成功", result);
        } catch (Exception e) {
            LOGGER.error("query shopping car is error", e);
            return new ApiResult(MessageResponse.QUERY_SHOPPING_CAR_ERROR);
        }
    }

    /**
     * 删除购物车物品
     *
     * @param shoppingCarId 购物车id
     * @param userId        用户id
     * @return
     */
    @DeleteMapping
    public ApiResult<Map<String, Object>> deleteShoppingCar(String shoppingCarId, String userId) {
        try {
            Map<String, Object> result = shoppingCarService.deleteShoppingCar(shoppingCarId, userId);
            LOGGER.info("delete shopping car is suceess...");
            return new ApiResult(200, "success", "删除成功", result);
        } catch (Exception e) {
            LOGGER.error("add shopping car is error", e);
            return new ApiResult(MessageResponse.DELETE_SHOPPING_CAR_ERROR);
        }
    }

    /**
     * 更新购物车信息
     *
     * @param infoDtoList 更新信息
     * @param userId      用户id
     * @return
     */
    @PutMapping
    public ApiResult<Map<String, Object>> updateShoppingCar(@RequestBody List<ShoppingCarInfoDto>
                                                                    infoDtoList, String userId) {
        try {
            Map<String, Object> result = shoppingCarService.updateShoppingCar(infoDtoList, userId);
            LOGGER.info("update shopping car is suceess...");
            return new ApiResult(200, "success", "更新购物车列表成功", result);
        } catch (Exception e) {
            LOGGER.error("update shopping car is error", e);
            return new ApiResult(MessageResponse.UPDATE_SHOPPING_CAR_ERROR);
        }
    }
}
